package com.wgts.wgts.mvp.view.activity.homapge;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wgts.wgts.R;
import com.wgts.wgts.mvp.view.activity.awardmore.AwardMoreActivity;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.guessing.GuessingActivity;
import com.wgts.wgts.mvp.view.activity.webactivity.WebActivity;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.tools.MoHuGlide;
import com.wgts.wgts.tools.Tools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HomePageActionEntryAdapter extends RecyclerView.Adapter<HomePageActionEntryAdapter.HomePageActionEntryViewHolder> {


    private Context context;

    private List<AppModleEntity.AppModleResult> scrDate;
    private Intent intent;

    public HomePageActionEntryAdapter(Context context, List<AppModleEntity.AppModleResult> scrDate) {
        this.context = context;
        this.scrDate = scrDate;
    }

    @NonNull
    @Override
    public HomePageActionEntryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new HomePageActionEntryViewHolder(LayoutInflater.from(context).inflate(R.layout.home_page_action_enrty_view_holder, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomePageActionEntryViewHolder homePageActionEntryViewHolder, final int i) {
        MoHuGlide.getInstance().raduice(context, scrDate.get(i).sourceUrl, homePageActionEntryViewHolder.imageView);
        if (i == scrDate.size()-1){
            Tools.setViewMargin(homePageActionEntryViewHolder.imageView, true, 10, 10, 0, 0);
        }
        homePageActionEntryViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(scrDate.get(i).actionUrlType.equals("0")){
                    intent = new Intent(context, WebActivity.class);
                    intent.putExtra(WebActivity.ACTION_URL, scrDate.get(i).actionUrl);
                }else{
                    try {
                        JSONObject jsonObject = new JSONObject(scrDate.get(i).actionUrl);
                        switch (jsonObject.getString("code")){
                            case "5":
                                intent = new Intent(context, JDGoodinfoActivity.class);
                                intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, jsonObject.getString("param"));
                                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                                break;
                            case "7":
                                intent = new Intent(context, AwardMoreActivity.class);
                                break;
                            case "8":
                                intent = new Intent(context, GuessingActivity.class);
                                break;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                if (!(context instanceof Activity)) {
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                if(intent != null){
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return scrDate.size();
    }

    public class HomePageActionEntryViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public HomePageActionEntryViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img_bg);
        }
    }
}
